package com.jason.m4399.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.m4399.ea.sdk.EASecurityCheck;
import com.m4399.ea.sdk.OnCheckListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.m4399.ea.api.ExclusiveAgent;
import cn.m4399.ea.spi.OnInitGlobalListener;

public class MainActivity extends AppCompatActivity {

    private static final String HOST = "https://m.4399api.com/openapiv2/open-md5.html";
    private static final String HOST_TEST = "https://m.4399api.com/openapiv2/open-md5.html?is_test=1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ExclusiveAgent.init(MainActivity.this, "131091", false, new OnInitGlobalListener() {

            // 激活状态回调函数
            @Override
            public void onActivationState(int code, String msg) {
                // code-> 0: 激活功能未配置
                // code-> 1: 激活成功
                // code-> 2: 已激活成功过

                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT)
                        .show();

                // 游戏方业务逻辑
            }

            // SDK 初始化成功回调函数
            @Override
            public void onInitComplete() {
                // 游戏方业务逻辑
                Toast.makeText(MainActivity.this, "初始化完成", Toast.LENGTH_SHORT)
                        .show();
            }
        });
        ExclusiveAgent.openGameHub(this);
    }


    public void onCheck(View view) {
        ThreadPoolUtil.execute(new Runnable() {
            @Override
            public void run() {
                String gameKey = "131091";
                String packageName = "com.XiaoXiao.Sunwards";
                String version = "1";
                String secret_key = "bd4753dfdaecde6b57bcfd351428cfa3";
                String md5 = "fa7b520a110acf2d1612ebfefd81afca";
                EASecurityCheck check = new EASecurityCheck();
                check.onCheck(gameKey, packageName, version, secret_key, md5, new OnCheckListener() {
                    @Override
                    public void onResult(int code, String message) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "code = "+code+",message = "+message, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }
        });
        /*ThreadPoolUtil.execute(new Runnable() {
            @Override
            public void run() {

                String gameKey = "131091";
                long timestamp = System.currentTimeMillis()/1000;
                String packageName = "com.XiaoXiao.Sunwards";
                String content = "/open-md5.html&";
                String version = "1";
                String secret_key = "bd4753dfdaecde6b57bcfd351428cfa3";
                String md5 = "fa7b520a110acf2d1612ebfefd81afca";
                content += timestamp+ "&";
                content += secret_key + "&";
                content += "gamekey=" + gameKey + "&";
                content += "md5=" + md5 + "&";
                content += "package=" + packageName + "&";
                content += "version=" + version;

                //从第11位开始，包含含11位，后面的十个字符(小写字母)
                String sign2 = Md5Utils.md5(content);
                String sign = sign2.substring(10,20);
                Log.i("EA", "sign2 = "+sign2+", sign = "+sign);
                Map<String, Object> param = new HashMap<>();
                param.put("gamekey", gameKey);
                param.put("package", packageName);
                param.put("version", version);
                param.put("md5", md5);
                param.put("time", timestamp+"");
                param.put("sign", sign);

                HttpUtils.requestPost(HOST, param,
                        new HttpUtils.OnHttpRequestListener() {
                            @Override
                            public void onResponseSuccess(String response) {
                                try {
                                    if (TextUtils.isEmpty(response)) {
                                        return;
                                    }

                                    JSONObject data = new JSONObject(response);
                                    int code = data.optInt("code");
                                    String msg = data.optString("message");
                                    Log.i("EA","code = "+code+", message = "+msg);
                                } catch (Exception e) {
                                    onResponseError(-1, e.getMessage());
                                }
                            }

                            @Override
                            public void onResponseError(final int code, final String errorMessage) {
                                Log.e("EA", errorMessage);
                            }
                        });
            }
        });*/
    }


}